package com.zxd.controller.backend;

import com.zxd.controller.common.CommonController;
import com.zxd.entity.Group;
import com.zxd.entity.vo.BaseVo;
import com.zxd.service.FileService;
import com.zxd.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * 文件管理
 * Created by 涂奕恒 on 2019/01/28.
 */
@Controller
@RequestMapping(value = "backend/file")
public class FileController extends CommonController {

    @Autowired
    private FileService fileService;

    @Autowired
    private GroupService groupService;

    @RequestMapping("/list")
    public String index(Model model, Integer groupId) {
        if (null == groupId || groupId == 0) {
            model.addAttribute("groupId", 0);
            model.addAttribute("level", 1);
        } else {
            Group group = groupService.getById(groupId);

            model.addAttribute("groupId", group.getId());
            model.addAttribute("level", group.getLevel());
        }
        return "backend/welcome";
    }

    @RequestMapping("/info")
    public String info() {
        return "backend/fileInfo";
    }

    @RequestMapping("/getData")
    @ResponseBody
    public BaseVo getData(String keyWord, Integer groupId, Integer page, Integer limit) {
        return fileService.getData(keyWord, groupId, page, limit);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(HttpServletRequest request, MultipartRequest multipartRequest, Integer groupId) {
        return fileService.saveInfo(request, multipartRequest, groupId);
    }

    /**
     * 删除文件信息
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer id) {
        try {
            fileService.deleteById(id);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除文件信息
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Integer batchDel(String ids) {
        try {
            return fileService.batchDel(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}